package org.top10.dao;

import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.top10.entity.CollectEntity;
import org.top10.entity.CollectRecord;
import org.top10.entity.WebSite;

public class DBServerImpl implements DBServer {
	
	private final static DBServer dbServer;
	private HibernateTemplate hibernateTemplate;
	private static final String InitConf="applicationContext*.xml";
	static{
		ApplicationContext context=new ClassPathXmlApplicationContext(InitConf);
		dbServer=(DBServer)context.getBean("dbServer");
	}
	
	public static DBServer getDbServer() {
		return dbServer;
	}
	
	public void delete(Object t) {
		hibernateTemplate.delete(t);
	}
	public Object get(Integer id,Class type) {
		return (Object)hibernateTemplate.get(type, id);
	}
	public WebSite getWebSiteByIdentifier(Integer webSizeID) {
		List<WebSite>ws= hibernateTemplate.find("from WebSite where webSizeID="+webSizeID);
		if(ws!=null){ 
			return ws.get(0);
		}
		return null;
	}
	public Integer insert(Object t) {
		return (Integer)hibernateTemplate.save(t);
		
	}
	
	public void update(Object t) {
		hibernateTemplate.update(t);
		
	}
	public List<Object>find(String sql) {
		return hibernateTemplate.find(sql);
	}
	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}
	public static void main(String[] args) {
			ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext*.xml");
			context.getBean("dbServer");
		}

	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}

	public void inserts(List<CollectEntity> entitys, CollectRecord record) {
		for(CollectEntity e:entitys){
			e.setCollRecord(record);
			insert(e);
		}
	}

}
